package com.scse.bms.web.client;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scse.bms.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.scse.bms.entity.FeedbackDO;

import java.util.List;

/**
 * @Title: FeedbackController
 * @Author: 吴赠禹
 * @Date: 2021/2/27 2:47
 * @Version: 1.0
 * @Description: 处理反馈操作
 */


@Slf4j
@Controller
public class FeedbackController {


    @Autowired
    private FeedbackService feedbackService;


    @PostMapping("/feedback/add")
    @ResponseBody
    public String addFeedback(@RequestBody FeedbackDO feedbackDO) {
        log.info("[addFeedback] 添加用户反馈：" + feedbackDO);
        feedbackDO.setCreated(DateUtil.now());
        boolean result = feedbackService.save(feedbackDO);
        log.info("[addFeedBack] result：" + result);
        return String.valueOf(result);
    }

    @GetMapping("/feedback/get")
    public String getFeedback(@RequestParam("id") Integer id, Model model) {
        FeedbackDO feedback = feedbackService.getById(id);
        log.info("[getFeedback] 获取用户反馈：" + feedback);
        model.addAttribute("feedback", feedback);
        return "back/reply";
    }

    @PostMapping("/feedback/delete")
    @ResponseBody
    public String deleteFeedback(@RequestBody FeedbackDO feedbackDO) {
        log.info("[deleteFeedback] 删除用户反馈：" + feedbackDO);
        boolean result = feedbackService.removeById(feedbackDO.getId());
        return String.valueOf(result);
    }

    @PostMapping("/feedback/update")
    @ResponseBody
    public String updateFeedback(@RequestBody FeedbackDO feedbackDO) {
        log.info("[updateFeedback] 修改用户反馈状态：" + feedbackDO);
        boolean result = feedbackService.updateById(feedbackDO);
        return String.valueOf(result);
    }


    @GetMapping("/feedback/list")
    @ResponseBody
    public String getFeedbackList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "6") Integer limit) {
        log.info("[getFeedbackList] 获取所有反馈");
        Page<FeedbackDO> page = new Page<>(pageNum, limit);
        // 这里获取没有连接查询，因此无效WrapperQuery
        Page<FeedbackDO> list = feedbackService.page(page);
        long total = list.getTotal();
        List<FeedbackDO> records = list.getRecords();
        JSONObject bean = new JSONObject();
        bean.put("code", 0);
        bean.put("msg", "success");
        bean.put("count", total);
        bean.put("data", records);
        return bean.toString();
    }

}
